import './index.css';

import { sleep } from '../../utils'

export class Application {
	constructor() {
		this.el = null;
		this.window = null;
		this.views = [];
		this.rootView = null;
		this.parent = null;
		this.done = true;
		this.init();


	}

	init() {
		this.el=document.createElement('div');
		this.el.classList.add('mp-application');
	}

	initRootView() {
		let view=this.el;
		this.rootView = this.el;
		view.parent = this;
		//添加视图
		view.el.classList.add('mp-native-view--instage');
		view.el.style.zIndex = 1;
		this.views.push(view);
		this.window.appendChild(view.el);
		view.viewDidLoad && view.viewDidLoad();
	}


	async presentView(view, useCache) {
		/* if (!this.done) {
			return;
		}
		this.done = false;

		const preView = this.views[this.views.length - 1]; */

		const preView = view;

		view.parent = this;
		view.el.style.zIndex = this.views.length + 1;
		view.el.classList.add('wx-native-view--before-present');
		view.el.classList.add('wx-native-view--enter-anima');
		preView.el.classList.add('wx-native-view--before-presenting');
		preView.el.classList.remove('wx-native-view--instage');
		preView.el.classList.add('wx-native-view--enter-anima');
		// preView.onPresentOut && preView.onPresentOut();
		view.onPresentIn && view.onPresentIn();
		!useCache && this.el.appendChild(view.el);
		this.views.push(view);
		!useCache && view.viewDidLoad && view.viewDidLoad();
		/* await sleep(20);
		preView.el.classList.add('wx-native-view--presenting');
		view.el.classList.add('wx-native-view--instage');
		await sleep(540);
		this.done = true; */
		view.el.classList.remove('wx-native-view--before-present');
		view.el.classList.remove('wx-native-view--enter-anima');
		preView.el.classList.remove('wx-native-view--enter-anima');
		preView.el.classList.remove('wx-native-view--before-presenting');
	}
	//关闭视图
	async dismissView(view) {
		console.log('dismissView关闭视图',window.electron);

		window.electron.ipcRenderer.send('current-window-close',view.appId);
	}
	//最小化视图
	async minimizeView(view) {
		console.log('minimizeView最小化视图',window.electron);

		window.electron.ipcRenderer.send('current-window-minimize',view.appId);
	}
}
